import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: { name: 'index' },
    // beforeEnter: (to, from, next) => {
    //   let data = localStorage.getItem('username')
    //   if (data) {
    //     console.log(222, to);
    //     if (to.name == 'home') {
    //       next({ name: 'about' })
    //     } else {
    //       next(false)
    //     }
    //   } else {
    //     if (to.name != 'home') {
    //       next({ name: 'home' })
    //     } else {
    //       next()
    //     }
    //   }
    // },
    children: [
      {
        // 首页
        path: '/index',
        name: 'index',
        component: () => import('../coms/index.vue')
      },
      {
        // 新闻中心
        path: '/news',
        name: 'news',
        component: () => import('../coms/news.vue')
      },
      {
        path: '/aus',
        name: 'aus',
        component: () => import('../coms/aus.vue')
      },
      {
        // 工作简介
        path: '/work',
        name: 'work',
        component: () => import('../coms/work.vue')
      },
      {
        // 联系我们
        path: '/contant',
        name: 'contant',
        component: () => import('../coms/contant.vue')
      },
    ]
  },
  {
    path: '/',
    name: 'about',
    
    component: () => import('../views/AboutView.vue'), 
    beforeEnter: (to, from, next) => {
      let data = localStorage.getItem('username')
      if (data) {
        console.log(222, to);
        if (to.name == 'about') {
          next({ name: 'home' })
        } else {
          next(false)
        }
      } else {
        if (to.name != 'about') {
          next({ name: 'about' })
        } else {
          next()
        }
      }
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router
